package com.zs4sdk.entity;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.common.library.entity.JSONResultEntity;
import com.common.library.util.Helper;

/**
 * 
 * function: 更新返回json类
 * 
 * @ author: aXiBa
 */
public class UpdateJSONResultEntity extends JSONResultEntity {

	public static final String TYPE_UPDATE_MENU = "1";
	public static final String TYPE_UPDATE_VERSION = "2";
	
	public static final String COLUMN_TYPE = "type";
	public static final String COLUMN_DATA = "data";
	public static final String COLUMN_IS_NOTICE = "is_notice";

	private String type;
	private String lastupdate;
	private String is_notice;
	
	ArrayList<MenuItemJSONResultEntity> menuEntityList;
	
	VersionJSONResultEntity versionEntity;
	
	/**
	 * 构造方法
	 */
	public UpdateJSONResultEntity(){
		
	}
	/**
	 * 构造方法
	 * @param json
	 */
	public UpdateJSONResultEntity(JSONObject json){
		this.fromJSON(json);
	}
	/**
	 * 构造方法
	 * @param jsonStr
	 */
	public UpdateJSONResultEntity(String jsonStr){
		try{
			this.fromJSON(new JSONObject(jsonStr));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public UpdateJSONResultEntity(UpdateJSONResultEntity entity){
		String type = entity.getType();
		this.type = type;
		this.is_notice = entity.getIs_notice();

		// 版本
		if(TYPE_UPDATE_VERSION.equals(type)) {
			this.versionEntity = entity.getVersionEntity();
		}
	}
	//#endregion 构造方法

	//#region Override方法
	@Override
	public JSONObject toJSON() {
		JSONObject result = super.toJSON();
		try {
			result.put(COLUMN_TYPE, this.type);
			result.put(COLUMN_IS_NOTICE, this.is_notice);
			

			// 版本
			if(TYPE_UPDATE_VERSION.equals(type)) {
				result.put(COLUMN_DATA, this.versionEntity);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public void fromJSON(JSONObject json) {
		super.fromJSON(json);
		if (Helper.isNotEmpty(json)){
			this.type = json.optString(COLUMN_TYPE);
			this.is_notice = json.optString(COLUMN_IS_NOTICE);
			

			// 版本
			if(TYPE_UPDATE_VERSION.equals(type)) {
				try {
					this.versionEntity = new VersionJSONResultEntity(json.getJSONObject(COLUMN_DATA));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 是否成功
	 */
	public boolean isSuccess(){
		return this.getResult() == 1;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getLastupdate() {
		return lastupdate;
	}
	public void setLastupdate(String lastupdate) {
		this.lastupdate = lastupdate;
	}
	public ArrayList<MenuItemJSONResultEntity> getMenuEntityList() {
		return menuEntityList;
	}
	public void setMenuEntityList(ArrayList<MenuItemJSONResultEntity> menuEntityList) {
		this.menuEntityList = menuEntityList;
	}
	public VersionJSONResultEntity getVersionEntity() {
		return versionEntity;
	}
	public void setVersionEntity(VersionJSONResultEntity versionEntity) {
		this.versionEntity = versionEntity;
	}
	public String getIs_notice() {
		return is_notice;
	}
	public void setIs_notice(String is_notice) {
		this.is_notice = is_notice;
	}
	
}
